package com.roads.site.controller;

import com.alibaba.fastjson.JSONObject;
import com.roads.common.annotation.Log;
import com.roads.common.constant.HttpStatus;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.page.TableDataInfo;
import com.roads.common.enums.BusinessType;
import com.roads.common.utils.http.HttpUtils;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.site.constant.VideoConstant;
import com.roads.site.domain.SiteVideo;
import com.roads.site.domain.SiteVideoUser;
import com.roads.site.service.ISiteVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 视频监控Controller
 * 
 * @author nhp
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/site/video")
public class SiteVideoController extends BaseController {

    @Autowired
    private ISiteVideoService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('site:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(SiteVideo model) {
        startPage();
        List<SiteVideo> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 查询用户视频监控列表
     */
    @PreAuthorize("@ss.hasPermi('site:video:list')")
    @GetMapping("/selectUserVideoList")
    public TableDataInfo selectUserVideoList(SiteVideoUser model) {
        model.setUser(getUsername());
        startPage();
        List<SiteVideoUser> list = service.selectUserVideoList(model);
        return getDataTable(list);
    }

    /**
     * 根据场景查询用户设置的视频监控
     *
     * @return 结果
     */
    @GetMapping(value = "/selectUserVideoListByType/{type}")
    public AjaxResult selectUserVideoListByType(@PathVariable("type") Integer type) {
        List<SiteVideo> list = service.selectUserVideoListByType(getUsername(), type);
        return AjaxResult.success(list);
    }

    /**
     * 查询用户视频监控未选择列表
     */
    @PreAuthorize("@ss.hasPermi('site:video:list')")
    @GetMapping("/selectUserVideoUnselectedList")
    public TableDataInfo selectUserVideoUnselectedList(SiteVideo model) {
        model.setCreateBy(getUsername());
        startPage();
        List<SiteVideo> list = service.selectUserVideoUnselectedList(model);
        return getDataTable(list);
    }



    /**
     * 导出
     */
    @PreAuthorize("@ss.hasPermi('site:video:export')")
    @Log(title = "视频监控", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SiteVideo model) {
        List<SiteVideo> list = service.selectModelList(model);
        ExcelUtils<SiteVideo> util = new ExcelUtils<SiteVideo>(SiteVideo.class);
        return util.exportExcel(list, "视频监控数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('site:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(service.getModelById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('site:video:add')")
    @Log(title = "视频监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SiteVideo model) {
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('site:video:edit')")
    @Log(title = "视频监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiteVideo model) {
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('site:video:remove')")
    @Log(title = "视频监控", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(service.deleteByIds(ids));
    }

    /**
     * 获取萤石云token
     */
    @GetMapping(value = "/getEzAccessToken")
    public AjaxResult getEzAccessToken() {
        String params = "appKey=" + VideoConstant.EZ_APP_KEY + "&appSecret=" + VideoConstant.EZ_APP_SECRET;
        String result = HttpUtils.sendPost(VideoConstant.EZ_APP_URL, params);
        // 将JSON字符串转换为Map
        Map<String, Object> map = JSONObject.parseObject(result);
        String code = map.get("code").toString();
        String token = "";
        if (String.valueOf(HttpStatus.SUCCESS).equals(code)) {
            JSONObject jsonObject = (JSONObject) map.get("data");
            token = (String) jsonObject.get("accessToken");
        }
        return success(token);
    }

    /**
     * 保存选择的视频监控
     *
     * @param type 应用场景
     * @param videoIds 视频监控ID
     * @return 结果
     */
    @PostMapping("/saveUserVideo")
    @Log(title = "视频监控", businessType = BusinessType.GRANT)
    public AjaxResult saveUserVideo(Integer type, String videoIds) {
        return toAjax(service.saveUserVideo(type, videoIds));
    }

    /**
     * 视频监控上移下移
     *
     * @param id 自增主键
     * @param type 上移下移
     * @return
     */
    @PostMapping("/moveVideo")
    public AjaxResult moveVideo(Integer id, String type) {
        return toAjax(service.moveVideo(id, type));
    }

}
